#include "../include/register.h"
#include "../Ui_set/ui_register.h"
#include<QRegularExpressionValidator>
#include<QMessageBox>
#include<QRandomGenerator>
#include<QDir>

Register::Register(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Register)
{
    ui->setupUi(this);
    this->setWindowTitle(tr("QQ"));
    this->setWindowIcon(QIcon(QPixmap("://images/qq.png")));
    this->setWindowModality(Qt::WindowModality::ApplicationModal);

    ui->userName->setFocus();
    QRegularExpressionValidator *password_rev=new QRegularExpressionValidator(QRegularExpression("^[\\w]{6,16}$"));
    ui->password->setValidator(password_rev);
    ui->password->setPlaceholderText(tr("6~16位，包含大小写字母和数字"));
    ui->password_2->setValidator(password_rev);

    connect(ui->registerBtn,SIGNAL(clicked()),this,SLOT(clickedRegiste()));
    connect(ui->quitBtn,SIGNAL(clicked()),this,SLOT(clickedQuit()));
}

Register::~Register()
{
    delete ui;
}

void Register::clickedRegiste()
{
    QString usrName=ui->userName->text();
    RegisterPassword=ui->password->text();
    QString pw=ui->password_2->text();
    if(usrName.isEmpty()){
        QMessageBox::critical(this,tr("错误"),tr("用户名不能为空！"),QMessageBox::Cancel);
        return;
    }
    if(RegisterPassword!=pw){
        QMessageBox::critical(this,tr("错误"),tr("密码设置不一致！"),QMessageBox::Cancel);
        ui->password_2->clear();
        return;
    }
    RegisterAccount=this->setRandomAccount();
    QFile *accountFile=new QFile("Account.txt");
    if(accountFile->open(QFile::ReadOnly)){
        QByteArray line_account_byte=accountFile->readLine();
        QString line_account_str=QString::fromUtf8(line_account_byte);
        if(RegisterAccount==line_account_str){
            QMessageBox::critical(this,tr("对不起！"),tr("分配的账号已存在，请修改后继续注册！"),QMessageBox::Cancel);
            accountFile->close();
            return;
        }
    }
    QMessageBox msgBox(this);
    msgBox.setText(tr("已生成您的账号，请牢记：\n   %1").arg(RegisterAccount));
    msgBox.setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel);
    auto btns=msgBox.buttons();
    QAbstractButton * okBtn;
    for(auto b:btns){
        if(b->text()=="OK"){
            okBtn=b;
            break;
        }
        //qDebug()<<b->text();
    }
    connect(okBtn,SIGNAL(clicked()),this,SLOT(saveAccountAndPassword()));
    connect(okBtn,SIGNAL(clicked(bool)),this,SLOT(saveUsrName()));
    accountFile->close();
    msgBox.exec();
}

void Register::saveAccountAndPassword()
{
    QFile* file=new QFile("Account.txt");
    if(file->open(QFile::WriteOnly|QFile::Append)){
        if(file->size()!=0&&file->atEnd())
            file->write("\n");
        file->write(RegisterAccount.toUtf8());
    }
    file->close();

    QDir usrFolder("./"+RegisterAccount);
    if(!usrFolder.exists()){
        QDir().mkdir(RegisterAccount);
        QFile *file=new QFile(RegisterAccount+"/Password.txt");
        if(file->open(QFile::WriteOnly)){
            file->write(RegisterPassword.toUtf8());
        }
        file->close();
    }
    this->close();
}

void Register::saveUsrName()
{
    usrName=ui->userName->text();
    if(usrName.isEmpty())   return;
    QFile *file=new QFile(RegisterAccount+"/usrName.txt");
    if(!file->open(QFile::WriteOnly))   return;
    file->write(usrName.toUtf8());
    file->close();
}

void Register::clickedQuit()
{
    //this->setWindowModality(Qt::WindowModality::NonModal);
    this->close();
}

const QString Register::setRandomAccount()
{
    QRandomGenerator rAccount=QRandomGenerator::securelySeeded();
    unsigned short randomDigit=rAccount.bounded(8,13);
    QString rac;
    for(int i=0;i!=randomDigit;i++)
        rac+=QString::number(rAccount.bounded(0,10));
    return rac;
}


